#include "lrcpage.h"
#include "ui_lrcpage.h"

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));

    //lrcPage添加动画效果
    lrcAnimation = new QPropertyAnimation(this,"geometry",this);
    lrcAnimation->setDuration(500);
    lrcAnimation->setStartValue(QRect(10,10,width(),height()));
    lrcAnimation->setEndValue(QRect(10,10+height(),width(),height()));

    connect(ui->hideBtn,&QPushButton::clicked,this,[=](){
        lrcAnimation->start();
    });

    // 动画结束时，将窗⼝隐藏
    connect(lrcAnimation,&QPropertyAnimation::finished,this,[=](){
        this->hide();
    });


}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrc(const QString &lrcPath)
{
    LyricLines.clear();

    //打开歌曲文件
    QFile lrcFile(lrcPath);
    if(!lrcFile.open(QFile::ReadOnly))
    {
        qDebug() << "打开歌曲文件失败" << endl;
        return false;
    }

    while(!lrcFile.atEnd())
    {
//        qDebug() << "进入歌词解析" << endl;
        QString lrcWord = lrcFile.readLine(1024);

        // [00:17.94]那些失眠的⼈啊 你们还好吗
        // [0:58.600.00]你像⼀只⻜来⻜去的蝴蝶
        int left = lrcWord.indexOf('[');
        int right = lrcWord.indexOf(']');

        // 解析时间
        qint64 lineTime = 0;
        int start = 0;
        int end = 0;
        QString time = lrcWord.mid(left,right - left + 1);

        // 解析分钟
        start = 1;
        end = time.indexOf(':');
        lineTime += lrcWord.mid(start,end - start).toInt()*60*1000;

        // 解析秒
        start = end+1;
        end = time.indexOf('.',start);
        lineTime += lrcWord.mid(start,end - start).toInt()*1000;

        // 解析毫秒
        start = end+1;
        end = time.indexOf('.',start);
        lineTime += lrcWord.mid(start,end - start).toInt();

        // 解析歌词
        QString word = lrcWord.mid(right+1).trimmed();
//        qDebug() << lineTime << word << endl;
        LyricLines.push_back(LyricLine(lineTime,word.trimmed()));
    }
    // 测试验证
    //    for(auto word : LyricLines)
    //    {
    //        qDebug() << word._time << word._text<<endl;
    //    }
    return true;
}

int LrcPage::getLineLrcWordIndex(qint64 pos)
{
    // 如果歌词是空的，返回-1
    if(LyricLines.isEmpty())
    {
        return -1;
    }
    if(LyricLines[0]._time > pos)
    {
        return 0;
    }

    // 通过时间⽐较，获取下标
    for(int i = 1;i < LyricLines.size();++i)
    {
        if(pos > LyricLines[i-1]._time && pos <= LyricLines[i]._time)
        {
            return i-1;
        }
    }

    // 如果没有找到，返回最后⼀⾏
    return LyricLines.size()-1;
}

QString LrcPage::getLineLrcWord(qint64 index)
{
    if(index < 0 || index >= LyricLines.size())
    {
        return "";
    }
    return LyricLines[index]._text;
}

void LrcPage::showLrcWord(int time)
{
    // 先要获取歌词--根据歌词的时间进⾏获取
    int index = getLineLrcWordIndex(time);
//    qDebug() << index << endl;
    if(index == -1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->linecenter->setText("当前歌曲⽆歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLineLrcWord(index-3));
        ui->line2->setText(getLineLrcWord(index-2));
        ui->line3->setText(getLineLrcWord(index-3));
        ui->linecenter->setText(getLineLrcWord(index));
        ui->line4->setText(getLineLrcWord(index+1));
        ui->line5->setText(getLineLrcWord(index+2));
        ui->line6->setText(getLineLrcWord(index+3));
    }
}

void LrcPage::setTitle(const QString &musicName, const QString &singer)
{
    ui->musicName->setText(musicName);
    ui->musicSinger->setText(singer);
}

